package com.youkeyi.ddy.cloud.application.business.hr.employee.education.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.BusinessEmployeeEducationHistoryCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.dto.BusinessEmployeeEducationHistoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.dto.BusinessEmployeeEducationHistoryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.param.BusinessEmployeeEducationHistoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.param.BusinessEmployeeEducationHistoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.param.BusinessEmployeeEducationHistoryListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.education.param.BusinessEmployeeEducationHistoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.HrEmployeeEducationHistoryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.dto.HrEmployeeEducationHistoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.param.HrEmployeeEducationHistoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.param.HrEmployeeEducationHistoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.param.HrEmployeeEducationHistoryQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.education.param.HrEmployeeEducationHistoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * BusinessEmployeeEducationHistoryController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 5:52 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeEducationHistoryController implements BusinessEmployeeEducationHistoryCloudService {

    @Autowired
    private HrEmployeeEducationHistoryCloudService hrEmployeeEducationHistoryCloudService;

    @Override
    public BusinessEmployeeEducationHistoryListCloudDto list(BusinessEmployeeEducationHistoryListCloudParam param) {
        BusinessEmployeeEducationHistoryListCloudDto dto = new BusinessEmployeeEducationHistoryListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeEducationHistoryQueryCloudParam employeeQueryCloudParam = new HrEmployeeEducationHistoryQueryCloudParam();
        employeeQueryCloudParam.setEmployeeEducationHistoryPrimaryKeys(param.getEmployeeEducationHistoryPrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeEducationHistoryListCloudDto employeeDtos = hrEmployeeEducationHistoryCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<BusinessEmployeeEducationHistoryListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeEducationHistoryListItemCloudDto item = new BusinessEmployeeEducationHistoryListItemCloudDto();
                item.setEmployeeEducationHistoryPrimaryKey(employee.getEmployeeEducationHistoryPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setStartDate(employee.getStartDate());
                item.setEndDate(employee.getEndDate());
                item.setSchoolName(employee.getSchoolName());
                item.setMajor(employee.getMajor());
                item.setEducationLevel(employee.getEducationLevel());
                item.setDegree(employee.getDegree());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeEducationHistoryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeEducationHistoryCreateCloudParam employeeCreateCloudParam = new HrEmployeeEducationHistoryCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setStartDate(param.getStartDate());
        employeeCreateCloudParam.setEndDate(param.getEndDate());
        employeeCreateCloudParam.setSchoolName(param.getSchoolName());
        employeeCreateCloudParam.setMajor(param.getMajor());
        employeeCreateCloudParam.setEducationLevel(param.getEducationLevel());
        employeeCreateCloudParam.setDegree(param.getDegree());
        employeeCreateCloudParam.setRemark(param.getRemark());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeEducationHistoryCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeEducationHistoryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeEducationHistoryPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeEducationHistoryUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeEducationHistoryUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeEducationHistoryPrimaryKey(param.getEmployeeEducationHistoryPrimaryKey());
        employeeUpdateCloudParam.setStartDate(param.getStartDate());
        employeeUpdateCloudParam.setEndDate(param.getEndDate());
        employeeUpdateCloudParam.setSchoolName(param.getSchoolName());
        employeeUpdateCloudParam.setMajor(param.getMajor());
        employeeUpdateCloudParam.setEducationLevel(param.getEducationLevel());
        employeeUpdateCloudParam.setDegree(param.getDegree());
        employeeUpdateCloudParam.setRemark(param.getRemark());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeEducationHistoryCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeEducationHistoryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeEducationHistoryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeEducationHistoryDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeEducationHistoryDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeEducationHistoryPrimaryKeys(param.getEmployeeEducationHistoryPrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeEducationHistoryCloudService.delete(employeeDeleteCloudParam);
    }
}
